from datetime import datetime

from appium import webdriver
from appium.options.android import UiAutomator2Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class CommonDriver():
    def get_driver(self):
        # 创建 UiAutomator2Options 实例，并设置各项参数
        options = UiAutomator2Options()
        options.platform_name = 'Android'
        options.platform_version = '7'
        options.app_package = 'com.dangdang.buy2'
        options.app_activity = 'com.dangdang.buy2.StartupActivity'
        options.no_reset = False
        options.unicode_keyboard = True
        options.udid = '127.0.0.1:21513'

        command_executor_url = 'http://127.0.0.1:4723/wd/hub'

        # 使用 options 参数代替 deprecated 的 desired_capabilities
        driver = webdriver.Remote(command_executor=command_executor_url, options=options)

        driver.implicitly_wait(10)

        print("连接成功")

        return driver


if __name__ == '__main__':
    driver = CommonDriver().get_driver()
    driver.find_element(By.XPATH, '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]').click()
    current_time = datetime.now()
    print("当前时间", current_time)
    try:
        element = WebDriverWait(driver, 20).until(
            EC.visibility_of_element_located(
                (By.XPATH, '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sur"]'))
        )
        element.click()
    except BaseException as e:
        print(e)
    finally:
        current_time = datetime.now()
        print("当前时间", current_time)

    # driver.find_element(By.XPATH, '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_sure"]').click()
    # driver.find_element(By.XPATH,
    #                     '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/dialog_cancel_tv"]').click()
    # driver.find_element(By.XPATH,
    #                     '//android.widget.FrameLayout[@resource-id="com.dangdang.buy2:id/home_search_bar_layout"]').click()
    # driver.find_element(By.XPATH, '//android.widget.EditText[@resource-id="com.dangdang.buy2:id/et_search"]').send_keys(
    #     "思考快与慢")
    # driver.find_element(By.XPATH, '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_search"]').click()
    # driver.find_element(By.XPATH,
    #                     '//android.widget.TextView[@resource-id="com.dangdang.buy2:id/etv_delete_key"]').click()
    # driver.quit()
    # try:
    #     screen_size = driver.get_window_size()
    #     width = screen_size["width"]  # 获取屏幕的宽度
    #     height = screen_size["height"]  # 获取屏幕的高度
    #
    #     start_x = width / 2
    #     start_y = height * 0.8
    #     end_x = width / 2
    #     end_y = height * 0.2
    #
    #     driver.swipe(start_x, start_y, end_x, end_y)
    #
    #     time.sleep(2)
    # finally:
    #     driver.quit()
    # driver.tap([(76, 525)], 500)
    # element = driver.find_element(By.XPATH, '//android.widget.TextView[@text="当当"]')
    #
    # action = TouchAction(driver)
    #
    # action.long_press(element).move_to(x=196, y=483).release().perform()
